package com.cc.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 新梦想.陈超
 * @version 2021.2
 * @Description: {TODO}
 * @date 2022/5/26 上午10:44
 */

@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    public LoginInterceptor(){
        System.out.println("登录拦截器创建了");
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle.......");
        //TODO 1.必须认证其登录了才能进入系统首页
        Object lgnUser = request.getSession().getAttribute("lgnUser");
        if(lgnUser!=null){
            return true;
        }
//        request.getRequestURI()///j14_project_crm
        log.info("uri:{},port:{},协议：{},host:{}",request.getRequestURI(),request.getServerPort(),request.getScheme(),request.getRemoteHost());
        //TODO 2.认证失败就将重定向到登录页面
        response.sendRedirect(request.getContextPath()+"/login/login.html");

        return false;//拦截，不会进入适配器 调用控制器的方法
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle.....");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion......");
    }
}
